﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace Beryl.OrderControllers.DBUpdate
{
    public class mainOrderUpdate
    {
        private static mainOrderUpdate instance = null;
        private static readonly object uniqueLock = new object();
        public static mainOrderUpdate Instance
        {
            get
            {
                lock (uniqueLock)
                {
                    if (instance == null)
                    {
                        instance = new mainOrderUpdate();
                    }
                }
                return instance;
            }
        }
        public mainOrderUpdate() { }

        // 更新主计划
        public void updateMainPlan(Dictionary<string, Dictionary<string, string>> planRes, Dictionary<string, DataRow> mainPlanObj, string cProductDepNo)
        {
            List<string> sqlArr = new List<string>();
            string sqlStr2 = "";
            try
            {
                // sql语句
                foreach (var cPOrderNo in planRes.Keys)
                {
                    if (mainPlanObj.ContainsKey(cPOrderNo))
                    {
                        string sqlStr0 = string.Format(@"UPDATE APS_MainPlan SET  
                                dMakePlanDate = CONVERT ( VARCHAR ( 10 ), GETDATE( ), 120 ),
                                cResGroupNo = '{0}',
                                dQty = {1},
                                cRgBegDate  = '{2}',
                                cRgEndDate  = '{3}',
                                cBegProDate = '{4}',
                                cEndProDate = '{5}',
                                cProductDepNo = '{6}'
                            WHERE
                                cPOrderNo = '{7}'", planRes[cPOrderNo]["cResGroupNo"], planRes[cPOrderNo]["dQty"], !planRes[cPOrderNo].ContainsKey("cRgBegDate") ? "" : planRes[cPOrderNo]["cRgBegDate"],
                                    !planRes[cPOrderNo].ContainsKey("cRgEndDate") ? "" : planRes[cPOrderNo]["cRgEndDate"],
                                    !planRes[cPOrderNo].ContainsKey("cBegProDate") ? "" : planRes[cPOrderNo]["cBegProDate"], !planRes[cPOrderNo].ContainsKey("cEndProDate") ? "" : planRes[cPOrderNo]["cEndProDate"]
                                    , cProductDepNo,cPOrderNo);
                        sqlArr.Add(sqlStr0);
                    }
                    else
                    {
                        
                        sqlStr2 += string.Format(@"('{0}','{1}',CONVERT ( VARCHAR ( 10 ), GETDATE( ), 120 ),'{2}',{3},'{4}','{5}','{6}','{7}','{8}'),", 
                            cPOrderNo, planRes[cPOrderNo]["cMatNo"], planRes[cPOrderNo]["cResGroupNo"], planRes[cPOrderNo]["dQty"], !planRes[cPOrderNo].ContainsKey("cRgBegDate") ? "" : planRes[cPOrderNo]["cRgBegDate"],
                            !planRes[cPOrderNo].ContainsKey("cRgEndDate") ? "" : planRes[cPOrderNo]["cRgEndDate"], !planRes[cPOrderNo].ContainsKey("cBegProDate") ? "" : planRes[cPOrderNo]["cBegProDate"],
                            !planRes[cPOrderNo].ContainsKey("cEndProDate") ? "" : planRes[cPOrderNo]["cEndProDate"], cProductDepNo);
                    }

                    string sqlStr00 = string.Format(@"UPDATE APS_PlanOrder SET 
                                cStatusNo = '0030', 
                                cStaUpdateTime = CONVERT ( VARCHAR ( 10 ), GETDATE( ), 120 ) 
                            WHERE 
                                cPOrderNo = '{0}'", cPOrderNo);
                    sqlArr.Add(sqlStr00);
                }
                if (sqlStr2 != "")
                {
                    sqlStr2 = sqlStr2.Substring(0, sqlStr2.Length - 1);
                    string sqlStr3 = "INSERT INTO APS_MainPlan(cPOrderNo,cMatNo,dMakePlanDate,cResGroupNo,dQty,cRgBegDate,cRgEndDate,cBegProDate,cEndProDate,cProductDepNo) VALUES " + sqlStr2;
                    sqlArr.Add(sqlStr3);
                }
                bool ok = CommonMethod.Instance.insertSqlList(sqlArr);
                if (!ok)
                {
                    throw new Exception("更新主计划失败");
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }
    }
}